/////////////////////////////////////////////////////////////////////////////
/*/	
*	Creator:		Alexander Kahl
*	Title:			
*	Last Edited:	07/16/2013
*	Summary:		
/*/
/////////////////////////////////////////////////////////////////////////////
#include "Turret.h"
#include "DefenseStruct.h"
#include "CSGD_TextureManager.h"
#include "EntityManager.h"
#include "SGD_Math.h"
#include <cassert>
#define RANGE 100
/////////////////////////////////////////////////////////////////////////////
// Function : 
// Notes	: 
/////////////////////////////////////////////////////////////////////////////
CTurret::CTurret(bool bTeamOne) : CDefenseStruct(bTeamOne) {
	m_eType = TURRET;

	SetImage(CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/Turret.png")));

	SetWidth(32);
	SetHeight(32);
	m_fCanonRotate = 0.5f * D3DX_PI;

	m_nDamage = 20;

	m_fFireTimer = 1.0f;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamic memory
/////////////////////////////////////////////////////////////////////////////
CTurret::~CTurret(){}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Parameters	: dT - Time elapsed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CTurret::Update(float dT){
	m_fFireTimer -= dT;
	vector<CMovingObject*> Units = CEntityManager::GetInstance()->m_BaseUnits;
	bool bTarget = false;

	for( unsigned int i = 0; i < Units.size(); ++i )
	{
		// Not a bullet or missile and not on the same team
		CBaseUnit* pUnit = dynamic_cast<CBaseUnit*>( Units[i] );
		if( pUnit && pUnit->GetTeam() != GetTeam() )
		{
			// In range
			if( pUnit->DistanceBetween(pUnit->GetPos(), GetPos()) < RANGE )
			{
				bTarget = true;
				tVector2D vOrientation = { 0, -1 };

				// Rotate Turret
				tVector2D vToUnit = {
					pUnit->GetPosX() - GetPosX(),
					pUnit->GetPosY() - GetPosY() };

				m_fCanonRotate = AngleBetweenVectors( vOrientation, vToUnit );

				// FIRE!
				if( m_fFireTimer <= 0 )
				{
					// CHANGE - Create bullet message?
					CBullet* bullet = new CBullet( this );
					bullet->SetImage( CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/Images/Bullet.png") ) );

					bullet->SetPosX( GetPosX() + GetWidth()/2 );
					bullet->SetPosY( GetPosY() + GetHeight()/2 );
					bullet->SetDeathY( (int)pUnit->GetPosY() + pUnit->GetHeight());
					bullet->SetWidth(8);
					bullet->SetHeight(8);
					bullet->SetPath(pUnit->GetPath());
					bullet->SetDamage( m_nDamage );

					vToUnit = Vector2DNormalize( vToUnit );
					bullet->SetVelX( vToUnit.fX * 100 );
					bullet->SetVelY( vToUnit.fY * 100 );

					CEntityManager::GetInstance()->AddEntity(bullet);

					m_fFireTimer = 1.0f;
				}
				break;
			}
		}
	}

	if( bTarget == false )
		m_fCanonRotate = 0.5f * D3DX_PI;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: Renders the generator to the screen
/////////////////////////////////////////////////////////////////////////////
void CTurret::Render(){
	// Draw base
	CDefenseStruct::Render();

	// Draw the cannon rotated
	RECT rCannon = { 32, 0, 64, 32 };
	CSGD_TextureManager::GetInstance()->Draw(
		GetImage(), (int)GetPosX(), (int)GetPosY() - 11,
		1.0f, 1.0f, &rCannon,
		16.0f, 27.0f, m_fCanonRotate );
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Upgrade
// Notes		: Increases stats based on level
/////////////////////////////////////////////////////////////////////////////
void CTurret::Upgrade(){
	m_nLevel++;
	//Increase turret health
	SetMaxHealth(GetMaxHealth() + 20);
	SetHealth(GetHealth() + 20);
	//Increase their attack
	m_nDamage += 5;
}